//Project: Juvenile justice
//Task: Create tables and figures
//Created by: Lucy Sorensen
//Date: June, 2024

//create lag measures
use "/network/rit/lab/ncerdc/data/cleandata/analysis_firstoffense.dta", clear
merge 1:1 mastid eoy using "/network/rit/lab/ncerdc/data/cleandata/lag_offenses_06to10.dta", gen(_lagoffmerge)
gen off_lag_miss = offense_lag==.
gen ser_lag_miss = serious_lag==.
gen off_lag2_miss = offense_lag2==.
gen ser_lag2_miss = serious_lag2==.
replace offense_lag = 0 if offense_lag==.
replace serious_lag = 0 if serious_lag==.
replace offense_lag2 = 0 if offense_lag2==.
replace serious_lag2 = 0 if serious_lag2==.
drop if _lagoffmerge==2

egen age_for_gr = median(age), by(grade)
replace age = age_for_gr if age==.

//group school disciplinary actions
gen most_serious = police==1|altschool==1|longoss==1
gen short_oss = oss==1
gen informal = conference==1|detention==1|warning==1|timeout==1|bus==1|corp==1|work==1|supervise==1
gen other = most_serious==0 & short_oss==0 & informal==0 & iss==0

sum complaint disorderly if male==0
sum complaint disorderly if white==1
sum complaint disorderly if ed_clean==0
sum complaint disorderly if ec_clean==0

//descriptives by juvenile complaint
eststo X : qui estpost tabstat read_z math_z daysabs after2_offenses after2_serious after2_complaint complaint disorderly offense_lag serious_lag off_lag_miss lo_report_z lo_refjc_mn_z male amind asian black hisp multi white ed_clean ec_clean lep_clean age grade elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll, by(complaint) statistics(mean sd)
esttab X using "/network/rit/lab/sorensenlab/Lucy/jj/stats_by_jj_first.csv", cells("read_z math_z daysabs after2_offenses after2_serious after2_complaint complaint disorderly offense_lag serious_lag off_lag_miss lo_report_z lo_refjc_mn_z male amind asian black hisp multi white ed_clean ec_clean lep_clean age grade elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll") plain nomtitle nonumber noobs replace

//Heatmap graph
local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
quietly reghdfe complaint c.lo_report_z c.lo_refjc_mn_z c.lo_report_z#c.lo_refjc_mn_z offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
margins, at(lo_report_z=(-1(.2)1) lo_refjc_mn_z=(-1(.2)1)) saving(/network/rit/lab/sorensenlab/Lucy/jj/margins1.dta, replace)
use /network/rit/lab/sorensenlab/Lucy/jj/margins1.dta, clear
set scheme cleanplots
twoway contour _margin _at1 _at2, xtitle("Police-court referral propensity (SDs)") ytitle("School-police referral propensity (SDs)") ztitle("Predicted probability juvenile complaint") levels(40) heatmap zlabel(#10, format(%9.3f))
graph export "/network/rit/lab/sorensenlab/Lucy/jj/jj_margins.png", replace

//complaint disparities
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_first.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_first.txt"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_reg.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_reg.txt"

foreach type in complaint disorderly {
quietly reghdfe `type' i.male offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_reg.xls", bdec(4) coefastr paren(se) 10pct append
margins i.male, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_first.xls", bdec(8) noaster noparen append
quietly reghdfe `type' i.race_cat offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_reg.xls", bdec(4) coefastr paren(se) 10pct append
margins i.race_cat, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_first.xls", bdec(8) noaster noparen append
quietly reghdfe `type' i.ed_clean offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_reg.xls", bdec(4) coefastr paren(se) 10pct append
margins i.ed_clean, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_first.xls", bdec(8) noaster noparen append
quietly reghdfe `type' i.ec_clean offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_reg.xls", bdec(4) coefastr paren(se) 10pct append
margins i.ec_clean, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_disparities_first.xls", bdec(8) noaster noparen append
}

//complaint disparities - intersectonal, margins graph
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.txt"
quietly reghdfe complaint i.male##i.race_cat offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
margins i.male#i.race_cat, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls", bdec(8) sdec(8) noaster noparen append
quietly reghdfe complaint i.male offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
margins i.male, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls", bdec(8) sdec(8) noaster noparen append
quietly reghdfe complaint i.race_cat offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
margins i.race_cat, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls", bdec(8) sdec(8) noaster noparen append
quietly reghdfe complaint i.ed_clean offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
margins i.ed_clean, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls", bdec(8) sdec(8) noaster noparen append

quietly reghdfe disorderly i.male##i.race_cat offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
margins i.male#i.race_cat, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls", bdec(8) sdec(8) noaster noparen append
quietly reghdfe disorderly i.male offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
margins i.male, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls", bdec(8) sdec(8) noaster noparen append
quietly reghdfe disorderly i.race_cat offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
margins i.race_cat, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls", bdec(8) sdec(8) noaster noparen append
quietly reghdfe disorderly i.ed_clean offense_lag serious_lag off_lag_miss i.primary_act, absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
margins i.ed_clean, atmeans post
outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/margins_sep2024.xls", bdec(8) sdec(8) noaster noparen append


//control for school punishment
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.txt"

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age
local susp_vars iss short_oss most_serious other

foreach var in read_z math_z daysabs {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars' `susp_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `susp_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean `susp_vars', absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `var'_lag `student_vars' `susp_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars' `susp_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `susp_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean `susp_vars', absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `student_vars' `susp_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_counterfactual.xls", bdec(4) coefastr paren(se) 10pct append
}

//interactions with school punishment
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/internal_interaction.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/internal_interaction.txt"

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age
local susp_vars iss short_oss most_serious other

foreach var in read_z math_z daysabs {
	quietly reghdfe `var' 1.complaint#1.informal 1.complaint#1.iss 1.complaint#1.short_oss 1.complaint#1.most_serious 1.complaint#1.other offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars' informal `susp_vars', absorb(off_month eoy grade) vce(cluster mastid) nocons
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_interaction.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' 1.complaint#1.informal 1.complaint#1.iss 1.complaint#1.short_oss 1.complaint#1.most_serious 1.complaint#1.other offense_lag offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars' informal `susp_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_interaction.xls", bdec(4) coefastr paren(se) 10pct append
}

//school punishment as outcome
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/internal_as_outcome.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/internal_as_outcome.txt"

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age
local susp_vars informal iss short_oss most_serious other

foreach var in `susp_vars' {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_as_outcome.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_as_outcome.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_as_outcome.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/internal_as_outcome.xls", bdec(4) coefastr paren(se) 10pct append
}

//main effects: 30 day window
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_30day.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_30day.txt"

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age

foreach var in after3_offenses after3_serious after3_complaint {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_30day.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_30day.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_30day.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_30day.xls", bdec(4) coefastr paren(se) 10pct append
}

//create manual dummies for decomposition
forval y = 2008/2010 {
	gen iyear`y' = eoy==`y'
}
forval m = 1/10 {
	gen imonth`m' = off_month==`m'
}
forval a = 1/99 {
	gen iact`a' = primary_act==`a'
}
forval g = 3/10 {
	gen igrade`g' = grade==`g'
}

//Gelbach decomposition
local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male black hisp amind multi asian ec_clean lep_clean age
foreach var in read_z math_z daysabs {
	b1x2 `var', x1all(ed_clean) x2all(complaint `school_vars' `student_vars') nofull nobase x2delta(g1=complaint : g2=black hisp amind multi asian : g3=male ec_clean lep_clean age : g4 = `school_vars')
}
local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male ed_clean ec_clean lep_clean age
foreach var in read_z math_z daysabs {
	b1x2 `var' if black==1|white==1, x1all(black) x2all(complaint) 
	b1x2 `var' if black==1|white==1, x1all(black) x2all(complaint `school_vars' `student_vars') nofull nobase x2delta(g1=complaint : g2=ed_clean : g3=ec_clean : g4= male lep_clean age : g5 = `school_vars')
}

foreach var in read_z math_z daysabs {
	b1x2 `var' if black==1|white==1, x1all(black) x2all(complaint)
	b1x2 `var', x1all(ed_clean) x2all(complaint)
}

levelsof schoolid, local(schools)
foreach school in `schools' {
	gen isch_`school' = schoolid==`school'
}

foreach var in read_z math_z daysabs {
	b1x2 `var' if black==1|white==1, x1all(black isch_*) x2all(complaint) x2delta(g1=complaint)
	b1x2 `var', x1all(ed_clean isch_*) x2all(complaint) x2delta(g1=complaint)
}

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
foreach var in read_z math_z daysabs {
	b1x2 `var' if black==1|white==1, x1all(black `school_vars') x2all(complaint) x2delta(g1=complaint)
	b1x2 `var', x1all(ed_clean `school_vars') x2all(complaint) x2delta(g1=complaint)
}

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
foreach var in read_z math_z daysabs {
	b1x2 `var' if black==1|white==1, x1all(black `school_vars') x2all(oss) x2delta(g1=oss)
	b1x2 `var', x1all(ed_clean `school_vars') x2all(oss) x2delta(g1=oss)
}

//main effects
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.txt"

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age

foreach var in read_z math_z daysabs {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `var'_lag `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
}

//effects of disorderly conduct
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.txt"
local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age
foreach var in read_z math_z daysabs {
	quietly reghdfe `var' disorderly offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' disorderly offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' disorderly i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' disorderly offense_lag serious_lag off_lag_miss ser_lag_miss `var'_lag `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' disorderly offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' disorderly offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars', absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' disorderly i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' disorderly offense_lag serious_lag off_lag_miss ser_lag_miss `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/disorderly_effects_first.xls", bdec(4) coefastr paren(se) 10pct append
}

//for only reportable offenses
capture drop reportable
gen reportable = 0
replace reportable = 1 if inlist(primary_act, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 43, 46, 47, 48, 49, 50, 87, 88, 90, 93, 94)
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.txt"

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age

foreach var in read_z math_z daysabs {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars' if reportable==1, absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' if reportable==1, absorb(i.schoolid##i.eoy grade off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean if reportable==1, absorb(mastid off_month i.eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `var'_lag `student_vars' if reportable==1, absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars' if reportable==1, absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' if reportable==1, absorb(i.schoolid##i.eoy off_month grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean if reportable==1, absorb(mastid off_month i.eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `student_vars' if reportable==1, absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_reportable.xls", bdec(4) coefastr paren(se) 10pct append
}

//by timing
capture drop time
gen time = 1 if inrange(off_month,8,9)
replace time = 2 if inrange(off_month,10,12)
replace time = 3 if inrange(off_month,1,3)
replace time = 4 if inrange(off_month,4,6)
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/effects_firstoffense_bytime.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/effects_firstoffense_bytime.txt"
local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age
forval l = 1/4 {
foreach var in read_z math_z daysabs {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars' if time==`l', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/effects_firstoffense_bytime.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars' if time==`l', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/effects_firstoffense_bytime.xls", bdec(4) coefastr paren(se) 10pct append
}
}

//by school level
capture destring level, force replace
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/effects_firstoffense_bylvl.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/effects_firstoffense_bylvl.txt"
local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age
forval l = 1/4 {
foreach var in read_z math_z daysabs {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars' if level==`l', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/effects_firstoffense_bylvl.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars' if level==`l', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/effects_firstoffense_bylvl.xls", bdec(4) coefastr paren(se) 10pct append
}
}

//reverse causality
sort mastid eoy
foreach var in read_z math_z daysabs {
	capture gen `var'_lag2=`var'[_n-2] if mastid==mastid[_n-2] & (eoy==eoy[_n-2]+2)
}
capture drop complaint_lag
sort mastid eoy
gen complaint_lag = complaint[_n-1] if mastid==mastid[_n-1]& (eoy==eoy[_n-1]+1)

capture erase "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.txt"
local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age

foreach var in read_z math_z daysabs {
	quietly reghdfe `var'_lag complaint offense_lag2 serious_lag2 off_lag2_miss ser_lag2_miss i.primary_act `var'_lag2 `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var'_lag complaint offense_lag2 serious_lag2 off_lag2_miss ser_lag2_miss i.primary_act `var'_lag2 `student_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var'_lag complaint i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var'_lag complaint offense_lag2 serious_lag2  off_lag2_miss ser_lag2_miss `var'_lag `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in offense serious {
	quietly reghdfe `var'_lag complaint offense_lag2 serious_lag2 off_lag2_miss ser_lag2_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var'_lag complaint offense_lag2 serious_lag2 off_lag2_miss ser_lag2_miss i.primary_act `student_vars', absorb(off_month i.schoolid##i.grade##i.eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var'_lag complaint i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month grade eoy) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var'_lag complaint offense_lag2 serious_lag2  off_lag2_miss ser_lag2_miss `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reverse_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
}

//Histograms of SPR and PCR
set scheme cleanplots
egen sch_mo_tag = tag(schoolid off_month)
hist lo_report_z if sch_mo_tag==1 & inrange(lo_report_z,-10,10), frequency xtitle("School-police referring propensity (SDs)") ytitle("Frequency (school-months)")
graph export "/network/rit/lab/sorensenlab/Lucy/jj/school_report_hist.png", replace
hist lo_refjc_mn_z if sch_mo_tag==1 & inrange(lo_refjc_mn_z,-10,10), frequency xtitle("Police-court referring propensity (SDs)") ytitle("Frequency (school-months)")
graph export "/network/rit/lab/sorensenlab/Lucy/jj/police_report_hist.png", replace


//Reduced form effects of SPR, PCR, and SPR*PCR
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/reduced_form_firstoffense.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/reduced_form_firstoffense.txt"
local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age
foreach var in read_z math_z daysabs {
	quietly reghdfe `var' lo_report_z lo_refjc_mn_z offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reduced_form_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' c.lo_report_z#c.lo_refjc_mn_z offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reduced_form_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' c.lo_report_z c.lo_refjc_mn_z c.lo_report_z#c.lo_refjc_mn_z offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reduced_form_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in complaint disorderly after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' lo_report_z lo_refjc_mn_z offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reduced_form_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' c.lo_report_z#c.lo_refjc_mn_z offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reduced_form_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' c.lo_report_z c.lo_refjc_mn_z c.lo_report_z#c.lo_refjc_mn_z offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/reduced_form_firstoffense.xls", bdec(4) coefastr paren(se) 10pct append
}

//testing differences across complaints/disorderly
sureg (complaint male offense_lag serious_lag off_lag_miss i.primary_act i.off_month i.schoolid i.grade i.eoy) (disorderly male offense_lag serious_lag off_lag_miss i.primary_act i.off_month i.schoolid i.grade i.eoy)
test [complaint]male=[disorderly]male

sureg (complaint black hisp amind asian multi offense_lag serious_lag off_lag_miss i.primary_act i.off_month i.schoolid i.grade i.eoy) (disorderly  black hisp amind asian multi offense_lag serious_lag off_lag_miss i.primary_act i.off_month i.schoolid i.grade i.eoy)
test [complaint]black=[disorderly]black
test [complaint]multi=[disorderly]multi

sureg (complaint ed_clean offense_lag serious_lag off_lag_miss i.primary_act i.off_month i.schoolid i.grade i.eoy) (disorderly ed_clean offense_lag serious_lag off_lag_miss i.primary_act i.off_month i.schoolid i.grade i.eoy)
test [complaint]ed_clean=[disorderly]ed_clean

//merge in principal chars
use "/network/rit/lab/ncerdc/data/cleandata/analysis_firstoffense.dta", clear
merge m:1 teachid using "/network/rit/lab/ncerdc/spwg/cleandata/personnel_through17.dta", gen(_principaldem)
drop if _principaldem==2


gen p_male = gender=="M"
gen p_black = 1 if ethnic=="B"
gen p_hisp = 1 if ethnic=="H"
gen p_amind = 1 if ethnic=="I"
gen p_other = 1 if ethnic=="O"
gen p_asian = 1 if ethnic=="A"
foreach var in p_black p_hisp p_amind p_other p_asian {
	replace `var' = 0 if ethnic!="" & `var'!=1
}
replace p_male = 0 if gender!="" & p_male!=1

reg lo_report_z p_male p_black p_hisp p_amind p_other p_asian
xtreg lo_report_z p_male p_black p_hisp p_amind p_other p_asian i.eoy i.grade, fe i(schoolid)

//effects using max severity offense
use "/network/rit/lab/sorensenlab/Lucy/jj/analysis_maxoffense.dta", clear
merge 1:1 mastid eoy using "/network/rit/lab/ncerdc/data/cleandata/lag_offenses_06to10.dta", gen(_lagoffmerge)
gen off_lag_miss = offense_lag==.
gen ser_lag_miss = serious_lag==.
gen off_lag2_miss = offense_lag2==.
gen ser_lag2_miss = serious_lag2==.
replace offense_lag = 0 if offense_lag==.
replace serious_lag = 0 if serious_lag==.
replace offense_lag2 = 0 if offense_lag2==.
replace serious_lag2 = 0 if serious_lag2==.
drop if _lagoffmerge==2

egen age_for_gr = median(age), by(grade)
replace age = age_for_gr if age==.

capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls"
capture erase "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.txt"

local school_vars elem middle high other_lvl titleone charter magnetsch alt special city suburb town rural enroll
local student_vars male amind asian black hisp multi white ed_clean ec_clean lep_clean age

foreach var in read_z math_z daysabs {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `var'_lag `student_vars', absorb(i.schoolid##i.eoy##i.grade off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `var'_lag `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls", bdec(4) coefastr paren(se) 10pct append
}
foreach var in after2_offenses after2_serious after2_complaint {
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars' `school_vars', absorb(off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss i.primary_act `student_vars', absorb(i.schoolid##i.grade##i.eoy off_month) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint i.primary_act ed_clean ec_clean lep_clean, absorb(mastid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls", bdec(4) coefastr paren(se) 10pct append
	quietly reghdfe `var' complaint offense_lag serious_lag off_lag_miss ser_lag_miss `student_vars', absorb(i.incident#i.schoolid off_month eoy grade) vce(cluster mastid)
	outreg2 using "/network/rit/lab/sorensenlab/Lucy/jj/complaint_effects_max.xls", bdec(4) coefastr paren(se) 10pct append
}


